package com.funo.logic.support;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.springframework.beans.BeanUtils;

import java.io.IOException;

/**
 * @author fantasy
 * @date 2024-08-08 15:22
 */
public class CommonDeserializer extends JsonDeserializer<Enum> {

    @Override
    public Enum deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {

        final String valueAsString = p.getValueAsString();
        final Object currentValue = p.getCurrentValue();
        final String currentName = p.getCurrentName();
        final Class<?> propertyType = BeanUtils.findPropertyType(currentName, currentValue.getClass());

        return (Enum) EnumsCodeMap.getValuedEnum(valueAsString,propertyType);
    }

}
